<?php

if (!defined("EXPONENT")) exit("");
// 9/16/2009: Immanuel192 write this action: update the cart
  global $db;
  
 $ticket=exponent_sessions_getTicketString();
 $cache=$db->selectObjectsBySql("
       SELECT {$db->prefix}album.*,
              {$db->prefix}album_format.name as format_name
              FROM  {$db->prefix}album  INNER JOIN {$db->prefix}album_format
                ON {$db->prefix}album_format.id={$db->prefix}album.format");
 $cache2=$db->selectObjectsBySql("SELECT {$db->prefix}order_cache.* FROM 
              {$db->prefix}order_cache WHERE ticket_string='{$ticket}'");
 // for each cache order, test that will we update or delete it?
 foreach ($cache as $key=>$value){
    // check quantity
    $quantity=0;
    if (isset($_POST['disk_'.$value->id])){
      $main_disk=intval($_POST['disk_'.$value->id]);
      if (isset($_POST['sheet_'.$value->id]))
        $sheet=intval($_POST['sheet_'.$value->id]);
      else $sheet = 0;
      if (isset($_POST['soundtrack_'.$value->id]))
        $soundtrack=intval($_POST['soundtrack_'.$value->id]);
      else $soundtrack = 0;
      
      $sql='';
      if ($main_disk >= 0) {
         $sql.="main_disk = {$main_disk}";
      }
      if ($sheet >= 0)
      {
        if ($sql != '') $sql.=',';
        $sql.="sheetbook = {$sheet}";
      }
      if ($soundtrack>= 0)
      {
        if ($sql != '') $sql.=',';
        $sql.="soundtrack= {$soundtrack}";
      }
      $flag=1;
     // echo "sql = ".$sql."<br>";
      foreach ($cache2 as $key2=>$value2)
       if ($value2->item_id == $value->id) { // this product is in cache
        if (($main_disk ==0)  && ($sheet==0) && ($soundtrack==0))
        // delete this row
          $db->sql("DELETE FROM {$db->prefix}order_cache WHERE ticket_string='{$ticket}' AND item_id={$value->id}");
        else {
        //  echo $sql;
          $db->sql("UPDATE {$db->prefix}order_cache SET {$sql} WHERE ticket_string='{$ticket}' AND item_id={$value->id}");
         }
        $flag=0;
        break; 
       }
      if (($flag == 1) && (($main_disk >0) ||  ($vocal>0) || ($sheet>0) || ($soundtrack>0)))
      {
        // add new product
        $new_product=null;
        $new_product->ticket_string=$ticket;
        $new_product->item_id=$value->id;
        $new_product->main_disk=$main_disk;
        $new_product->sheetbook=$sheet;
        $new_product->vocal=$vocal;
        $new_product->soundtrack=$soundtrack;
        $new_product->add_date=time();
        $db->insertObject($new_product,"order_cache");
        
      }
    }    
 }
 exponent_flow_redirect();
?>
